#include "PhysicsCalculate.h"



b2Vec2 PhysicsCalculate::getSpeedEnergyBalance( const b2Vec2 &vecOldSpeed,const b2Vec2 &collisonVec )
{
	b2Vec2 vecNewSpeed;
	if (collisonVec.x<=0.00000001||collisonVec.y<=0.00000001)
	{
		vecNewSpeed.x=vecOldSpeed.x;
		vecNewSpeed.y=vecOldSpeed.y;
	}
	else
	{

		//					 (Vox*Vox+Voy*Voy)*(collisonX*collisonX)
		//				__________________________________________________
		//	Vnx*Vnx* =       collisonX*collisonX+collisonY*collisonY    
		vecNewSpeed.x=sqrt( (vecOldSpeed.x*vecOldSpeed.x+vecOldSpeed.y*vecOldSpeed.y)*(collisonVec.x*collisonVec.x)/(collisonVec.x*collisonVec.x+collisonVec.y*collisonVec.y) );
		//			collisonY*Vnx
		//		________________________
		// Vny=		 collisonX
		vecNewSpeed.y=(collisonVec.y*vecNewSpeed.x)/collisonVec.x;

	}


	vecNewSpeed.x=(collisonVec.x*vecNewSpeed.x<0)?(-1*vecNewSpeed.x):vecNewSpeed.x;
	vecNewSpeed.y=(collisonVec.y*vecNewSpeed.y<0)?(-1*vecNewSpeed.y):vecNewSpeed.y;
	return vecNewSpeed;
}

b2Vec2 PhysicsCalculate::getPositionAdd( const b2Vec2 &vecOldSpeed,const b2Vec2 &vecGSpeed,float fTime )
{
	b2Vec2 vecPosition;
	vecPosition.x=(2*vecOldSpeed.x+vecGSpeed.x*fTime)*fTime/2;
	vecPosition.y=(2*vecOldSpeed.y+vecGSpeed.y*fTime)*fTime/2;
	return vecPosition;
}